package org.example;

import java.util.Random;
import java.util.concurrent.*;

/**
 *   CompletableFuture,如果使用我们提供的线程池,此时,supplier是被前台线程执行
 *                                    如果我们没有提供线程池,此时,supplier是被守护线程执行
 *
 *   https://mp.weixin.qq.com/s?__biz=MzI4Njc5NjM1NQ==&mid=2247523935&idx=1&sn=3322482ca450a354e97770331057bdf9&chksm=ebd5a173dca22865c195cc14c01a68ed9c2ceee561d7cf2b7282f72ebef5cb90cbdcc63e3a22&scene=27
 */
public class AppCompletableFuture {
    //主线程是前台线程
    public static void main(String[] args) throws Exception {

        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.submit(new Callable<Object>() {
            @Override
            public Object call() throws Exception {
                System.out.println("Callable中是否为守护线程 :" + Thread.currentThread().isDaemon()
                +"---线程是:"+Thread.currentThread().getName());
                return null;
            }
        });


        final CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
            // true 为守护线程.  此时用系统提供的ForkJoinPool
            System.out.println("无线程池的supplyAsync是否为守护线程 " + Thread.currentThread().isDaemon()
                +"----线程是:" + Thread.currentThread().getName()
            );



//            try {
//                TimeUnit.SECONDS.sleep(3);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
            System.out.println("this lambda is executed by forkJoinPool");
            return "resultWithDefaultPool";
        });

        final CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            // false 不为守护线程
            System.out.println("有线程池的supplyAsync,使用executorService 时是否为守护线程 : " + Thread.currentThread().isDaemon()
                +"----线程是:"+Thread.currentThread().getName()
            );
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("this lambda is executed by 提供的线程池");
            return "resultWithMyPool";
        }, executorService);



        System.out.println("主线程开始,判断主线程是否为守护线程:"+ Thread.currentThread().isDaemon());
        System.out.println(completableFuture.get());
        System.out.println(future.get());
        executorService.shutdown();
        System.out.println("主线程结束了-------");
    }

    public static void longTimeMethod() {
        Long _long = 0L;
        long start = System.currentTimeMillis();
        for (long i = 0; i <= 199999999L; i++) {
            Random random = new Random();
            int seed = random.nextInt(100);
            Random random1 = new Random(seed);
            int i1 = random1.nextInt(100);
            _long = i1 + _long;

            if (_long % 16000000 == 0) {
                System.out.println(_long + "----无线程池");
            }
        }
        long end = System.currentTimeMillis();

        System.out.println("运行的结果是:" + _long);
    }
}
